<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// blog子域名绑定到blog模块

use think\ROute;
use phpmailer\phpmailer;
// 完整域名绑定到admin模块
Route::domain('m.plikeland.com','wap');



// 应用公共文件
/**身份证号码验证**/
function getValidateCode($id17)
{
    $weight = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //十七位数字本体码权重
    $validate = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); //mod11,对应校验码字符值
    $sum = 0;
    $mode = 0;
    for ($i = 0; $i < strlen($id17); $i++) {
        $sum = $sum + (int)$id17[$i] * $weight[$i];
    }
    $mode = $sum % 11;
    return $validate[$mode];
}

function validateCode($idNo)
{
    if (strlen($idNo) != 18) return false;
    $weight = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //十七位数字本体码权重
    $validate = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); //mod11,对应校验码字符值
    $sum = 0;
    for ($i = 0; $i < 17; $i++) {
        $sum = $sum + (int)$idNo[$i] * $weight[$i];
    }
    $mode = $sum % 11;
    return (substr($idNo, -1, 1) == $validate[$mode]);
}

function getHongKongValidateCode($idNo, $fist_number)
{
    $check_code = ($fist_number * 8);
    $i = 7;
    $idStr = substr($idNo, 1, 6);
    for ($j = 0; $j < 6; $j++) {
        $check_code += (int)$idStr{$j} * $i;
        $i -= 1;

    }
    $remainder = $check_code % 11;
    switch ($remainder) {
        case 0:
            $last_key = 0;
            break;
        case 1:
            $last_key = 'A';
            break;
        default:
            $last_key = 11 - $remainder;
            break;
    }
    return $last_key;
}

/* 2016-11-22
 * 验证身份证号码
 * 中国内陆身份证、中国香港身份证*/
function getChinaValidateCode($idNo)
{
    $arr = array();
    switch (strlen($idNo)) {
        case 18:
            //内地身份证
            $last_card = substr($idNo, 17, 1);
            $id_card = substr($idNo, 0, 17);
            $chk_last_card = getValidateCode($id_card);
            if ($chk_last_card !== $last_card) {
                return false;
            }
            //根据身份证号码区别男女
            $arr['sex'] = 0 == (substr($id_card, 16, 1)) % 2 ? 2 : 1;
            //根据身份证获取生日
            $arr['birthday'] = (substr($id_card, 6, 8));
            $arr['id_card'] = $idNo;
            $arr['check_code'] = $chk_last_card;
            break;
        case 8:
            //港澳
            $fist_card = substr($idNo, 0, 1);
            $fist_number = ord(strtoupper($fist_card)) - 64;
            if (is_numeric($fist_card)) {
                //澳门身份证，暂时不做
                return false;
            } elseif (0 < $fist_number && $fist_number < 27) {
                //香港身份证
                $last_card = (int)substr($idNo, 7, 1);
                $chk_last_card = getHongKongValidateCode($idNo, $fist_number);
                if ($chk_last_card !== $last_card) {
                    return false;
                }
                $arr['sex'] = 1;
                $arr['birthday'] = 0;
                $arr['id_card'] = $idNo;
                $arr['check_code'] = $chk_last_card;
            } else {
                return false;
            }
            break;
        default:
            return false;
    }
    return $arr;
}

function getIp() {
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
    else
        if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else
            if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
                $ip = getenv("REMOTE_ADDR");
            else
                if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
                    $ip = $_SERVER['REMOTE_ADDR'];
                else
                    $ip = "unknown";
    return ($ip);
}

function GetrealIP()
{
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
    else
        if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else
            if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
                $ip = getenv("REMOTE_ADDR");
            else
                if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
                    $ip = $_SERVER['REMOTE_ADDR'];
                else
                    $ip = "unknown";
    return ($ip);
}

function menu_tree($data, $pid = 0, $count = 0) {
    $treeList = array();
    foreach ($data as $key => $value){
        $value['level'] = $count;
        if($value['pid'] == $pid){
            foreach ($data as $k => $v) {
                if($v['pid'] == $value['id']){
                    $value['child'] = menu_tree($data, $value['id'], $count+1);
                    break;
                }else{
                    $value['child'] = '';
                }
            }
            $treeList[] = $value;
        }
    }
    return $treeList ;
}
//加密
function ScriptCode($id,$ctime){
    $str = $ctime.','.$id.','.$id;

    $str2=str_replace('=','',base64_encode(base64_encode($str)));

    return $str2;

}
//解密
function getScriptCode($code){
    $str = base64_decode(base64_decode($code));


    $str1 = explode(',',$str);

    return $str1[2];
}
//生成专题html
function makehtml(){
    $filename=rand(1000,9999).date(Ymd);
}

function send_mail($title,$content){
    $sendmail = '83631113@qq.com'; //发件人邮箱
    $sendmailpswd = "lqlovmnucayzbgig"; //客户端授权密码,而不是邮箱的登录密码！
    $send_name = '22';// 设置发件人信息，如邮件格式说明中的发件人，
    $toemail = '83631113@qq.com';//定义收件人的邮箱
    $to_name = '111';//设置收件人信息，如邮件格式说明中的收件人
    $mail = new PHPMailer();
    $mail->isSMTP();// 使用SMTP服务
    $mail->CharSet = "utf8";// 编码格式为utf8，不设置编码的话，中文会出现乱码
    $mail->Host = "smtp.qq.com";// 发送方的SMTP服务器地址
    $mail->SMTPAuth = true;// 是否使用身份验证
    $mail->Username = $sendmail;//// 发送方的
    $mail->Password = $sendmailpswd;//客户端授权密码,而不是邮箱的登录密码！
    $mail->SMTPSecure = "ssl";// 使用ssl协议方式
    $mail->Port = 465;//  qq端口465或587）
    $mail->setFrom($sendmail,$send_name);// 设置发件人信息，如邮件格式说明中的发件人，
    $mail->addAddress($toemail,$to_name);// 设置收件人信息，如邮件格式说明中的收件人，
    $mail->addReplyTo($sendmail,$send_name);// 设置回复人信息，指的是收件人收到邮件后，如果要回复，回复邮件将发送到的邮箱地址
    //$mail->addCC("xxx@qq.com");// 设置邮件抄送人，可以只写地址，上述的设置也可以只写地址(这个人也能收到邮件)
    //$mail->addBCC("xxx@qq.com");// 设置秘密抄送人(这个人也能收到邮件)
    //$mail->addAttachment("bug0.jpg");// 添加附件
    $mail->Subject = $title;// 邮件标题
    $mail->Body = $content;// 邮件正文
    //$mail->AltBody = "This is the plain text纯文本";// 这个是设置纯文本方式显示的正文内容，如果不支持Html方式，就会用到这个，基本无用
    $mail->send();
}